/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: CodeUtils.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.baselib.network.Utils;

import android.content.Context;
import android.text.TextUtils;

import io.bhex.baselib.network.response.BaseResponse;
import io.bhex.baselib.utils.ToastUtils;
import io.bhex.sdk.BhexSdk;
import io.bhex.sdk.account.bean.enums.ERROR_CODE;


public class CodeUtils {
    /**
     * 判断返回数据 data不为空 并且 code 和msg都不为空 返回true
     * @param data
     * @return
     */
    public static boolean isSuccess(BaseResponse data){
        if (data != null) {
            return (TextUtils.isEmpty(data.getCode()) || data.getCode().equalsIgnoreCase("200") || data.getCode().equalsIgnoreCase("0"))
                    && TextUtils.isEmpty(data.getMsg());
        }

        return false;
    }

    /**
     * data不为空 并且 code 和msg都不为空 返回true
     * @param data
     * @return
     */
    public static boolean isSuccess(BaseResponse data, boolean isToastErrorMsg){
        if (data != null) {
            if ((TextUtils.isEmpty(data.getCode()) || data.getCode().equalsIgnoreCase("200") || data.getCode().equalsIgnoreCase("0"))&&TextUtils.isEmpty(data.getMsg())) {
                return true;
            }else {
                if (isToastErrorMsg) {
                    String msg = data.getMsg();
                    if (!TextUtils.isEmpty(msg)) {
                        ToastUtils.showShort(msg);
                    }else{
                        String code = data.getCode();
                        ToastUtils.showShort("Please try again later [" + code +"]");
                    }
                }
            }
        }

        return false;
    }


    /**
     * data不为空 并且 code 和msg都不为空 返回true
     * @param data
     * @return
     */
    public static boolean isSuccess(Context context, BaseResponse data, boolean isToastErrorMsg, boolean isNeedLogin){
        if (data != null) {
            if ((TextUtils.isEmpty(data.getCode()) || data.getCode().equalsIgnoreCase("200") || data.getCode().equalsIgnoreCase("0"))&&TextUtils.isEmpty(data.getMsg())) {
                return true;
            }else {
                if (isToastErrorMsg) {
                    String msg = data.getMsg();
                    if (!TextUtils.isEmpty(msg)) {
                        ToastUtils.showShort(msg);
                    }else{
                        String code = data.getCode();
                        ToastUtils.showShort("Please try again later [" + code +"]");
                    }
                }

                if (isNeedLogin&&data.getCode().equals(ERROR_CODE.NO_LOGIN.getCode())) {
                    //IntentUtils.goLogin(context,"");
                    BhexSdk.NoCookieNotity(context,"");
                }
            }
        }

        return false;
    }

    /**
     * data不为空 并且 code 和msg都不为空 返回true
     * @param data
     * @return
     */
    public static boolean isSuccessAutoLogin(Context context, BaseResponse data, boolean isToastErrorMsg, String caller){
        if (data != null) {
            if ((TextUtils.isEmpty(data.getCode()) || data.getCode().equalsIgnoreCase("200") || data.getCode().equalsIgnoreCase("0"))&&TextUtils.isEmpty(data.getMsg())) {
                return true;
            }else {
                if (isToastErrorMsg) {
                    String msg = data.getMsg();
                    if (!TextUtils.isEmpty(msg)) {
                        ToastUtils.showShort(msg);
                    }else{
                        String code = data.getCode();
                        ToastUtils.showShort("Please try again later [" + code +"]");
                    }
                }
                if (data.getCode().equals(ERROR_CODE.NO_LOGIN.getCode())) {
                    //IntentUtils.goLogin(context,caller);
                    BhexSdk.NoCookieNotity(context, caller);
                }
            }
        }

        return false;
    }

    /**
     * 登录
     * @param data
     * @return
     */
    public static boolean isLogonExpires(BaseResponse data){
        if (data != null) {
            if (!TextUtils.isEmpty(data.getCode())){
                return data.getCode().equals(ERROR_CODE.NO_LOGIN.getCode());
            }
        }
        return false;
    }

//    public static boolean isResponseFailedMsg(BaseResponse data){
//        if (data != null&& !TextUtils.isEmpty(data.getCode())) {
//            ToastUtils.showShort(data.getMsg());
//            return true;
//        }else{
//            return false;
//        }
//    }
}
